﻿using JavaDevToys.Rcl.Context;
using JavaDevToys.Rcl.Models;
using JavaDevToys.Rcl.Service;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Windows.UI.Popups;

namespace JavaDevToys.Platforms.Windows.Service.Impl
{
    public class SettingService : ISettingService
    {
        public HisDbContext dbContext { get; set; }

        public SettingService(HisDbContext dbContext)
        {
            this.dbContext = dbContext;
        }

        public SettingInfo GetSettings()
        {
            string json=string.Empty;
            if(!Directory.Exists(Path.Combine(FileSystem.AppDataDirectory, "Config")))
            {
                Directory.CreateDirectory(Path.Combine(FileSystem.AppDataDirectory, "Config"));
            }
            if(!File.Exists(Path.Combine(FileSystem.AppDataDirectory, "Config/Settings.Json")))
            {
                File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/Settings.Json"), Path.Combine(FileSystem.AppDataDirectory, "Config/Settings.Json"));
            }
            else
            {
                //File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/Settings.Json"), Path.Combine(FileSystem.AppDataDirectory, "Config/Settings-New.Json"),true);
            }
            using (StreamReader sr = new StreamReader(Path.Combine(FileSystem.AppDataDirectory, "Config/Settings.Json"), Encoding.UTF8))
            {
                json = sr.ReadToEnd();
            }
            SettingInfo oldSetting = JsonSerializer.Deserialize<SettingInfo>(json);
            var projects = dbContext.DevtoysProjects.ToList();
            foreach (var project in projects)
            {
                if (!oldSetting.JavaProjectInfoList.Any(a => a.Name == project.ProjectName))
                {
                    oldSetting.JavaProjectInfoList.Add(new JavaProjectInfo()
                    {
                        Desc = project.ProjectDesc,
                        Name = project.ProjectName,
                    });
                }
                oldSetting.JavaProjectInfoList.FirstOrDefault(f => f.Name == project.ProjectName).ProjectType= project.ProjectType;
                oldSetting.JavaProjectInfoList.FirstOrDefault(f => f.Name == project.ProjectName).sqlserverInfo = new SqlserverInfo()
                {
                    Database = project.ProjectDatabase,
                    Port = project.ProjectPort,
                    Pwd = project.ProjectPwd,
                    Schemas = project.ProjectSchemas,
                    Server = project.ProjectServer,
                    UserName = project.ProjectUsername
                };
            }
            return oldSetting;
        }

        public void SaveSettings(SettingInfo settingInfo)
        {
            try
            {
                string json = JsonSerializer.Serialize(settingInfo);
                File.WriteAllText(Path.Combine(FileSystem.AppDataDirectory, "Config/Settings.Json"), json);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public SettingInfo GetSettings(int projectType)
        {
            SettingInfo settingInfo= GetSettings();
            settingInfo.JavaProjectInfoList= settingInfo.JavaProjectInfoList.Where(w=>w.ProjectType==projectType).ToList();
            return settingInfo;
        }

        public void SaveInterfaceSetting(ExInterfaceSetting exInterfaceSetting)
        {
            try
            {
                string json = JsonSerializer.Serialize(exInterfaceSetting);
                File.WriteAllText(Path.Combine(FileSystem.AppDataDirectory, "Config/InterfaceTypeMapping.Json"), json);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public ExInterfaceSetting GetInterfaceSetting()
        {
            string json = string.Empty;
            if (!Directory.Exists(Path.Combine(FileSystem.AppDataDirectory, "Config")))
            {
                Directory.CreateDirectory(Path.Combine(FileSystem.AppDataDirectory, "Config"));
            }
            if (!File.Exists(Path.Combine(FileSystem.AppDataDirectory, "Config/InterfaceTypeMapping.Json")))
            {
                return new ();
            }

            using (StreamReader sr = new StreamReader(Path.Combine(FileSystem.AppDataDirectory, "Config/InterfaceTypeMapping.Json"), Encoding.UTF8))
            {
                json = sr.ReadToEnd();
            }
            return JsonSerializer.Deserialize<ExInterfaceSetting>(json);
        }
    }
}
